﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESMAM.Model;
using ESMAM.Tools;

namespace ESMAM.DAL.SYS
{
    public class LangDAL : ESMAM.DAL.DAO.DAL
    {
        public Pager GetPager(ReqParam req)
        {
            var kv = DbTop.GetDataTableByPager(req.CurrentPage, req.PageSize, "*", "sys_Dict_Lang ", req.Where, req.Orders);
            Pager pager = new Pager()
            {
                list = kv.Key,
                pageCurrent = req.CurrentPage,
                pageSize = req.PageSize,
                totalRow = kv.Value
            };
            return pager;
        }

        public KeyValuePair<bool,string> Upsert(NameValueCollection nvc)
        {
            var kv = DbTop.Save(nvc, "sys_dict_lang", "id");

            string msg = string.Empty;

            if (kv.Key == MJTop.Data.SaveType.Insert)
            {
                if (kv.Value)
                {
                    msg = "添加成功！";
                }
                else
                {
                    msg = "添加失败！";
                }
            }
            else
            {
                if (kv.Value)
                {
                    msg = "更新成功！";
                }
                else
                {
                    msg = "更新失败！";
                }
            }
            return new KeyValuePair<bool, string>(kv.Value, msg);
        }

        public bool Delete(string id)
        {
            string del_sql = string.Format("delete from sys_dict_lang where id =" + id);
            return DbTop.ExecSql(del_sql) > 0;
        }

        public bool SetLang(string uid, Lang lg)
        {
            string upSql = "Update sys_user Set ShiYYY=N'{1}' Where ID='{0}'";
            upSql = string.Format(upSql, uid, lg.ToString());

            return DbTop.ExecSql(upSql) > 0;
        }


        public DataTable GetAll()
        {
            var data = DbTop.GetDataTable("select * from sys_dict_lang");

            
            return data;
        }

        public bool FanYin(string type, bool isReplace)
        {
            var lstEnum = Lang.en.GetListEnums();
            if (type.Equals("menu", StringComparison.OrdinalIgnoreCase))
            {
                if (isReplace)
                {
                    var data = DbTop.GetDataTable("Select name From SYS_Menu Where length(ifnull(name,''))>0");
                    var lstWords = data.MapperList<string>("name");
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }
                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_Menu Set " + lg + "Name=N'" + lstNewWord[j] + "' Where NAME=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
                else
                {
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }

                        var data = DbTop.GetDataTable("Select name From SYS_Menu Where length(ifnull(name,''))>0 and length(ifnull(" + lg + "name,''))<=0 ");
                        var lstWords = data.MapperList<string>("name");

                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_Menu Set " + lg + "Name=N'" + lstNewWord[j] + "' Where NAME=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
            }
            else if (type.Equals("lang", StringComparison.OrdinalIgnoreCase))
            {
                if (isReplace)
                {
                    var data = DbTop.GetDataTable("Select zh From sys_Dict_Lang Where length(ifnull(zh,''))>0");
                    var lstWords = data.MapperList<string>("zh");
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }
                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_Dict_Lang Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
                else
                {
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }

                        var data = DbTop.GetDataTable("Select zh From sys_Dict_Lang Where length(ifnull(zh,''))>0 and length(ifnull(" + lg + ",''))<=0 ");
                        var lstWords = data.MapperList<string>("zh");

                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_Dict_Lang Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
            }
            else if (type.Equals("wordtype", StringComparison.OrdinalIgnoreCase))
            {
                if (isReplace)
                {
                    var data = DbTop.GetDataTable("Select zh From sys_word_type Where length(ifnull(zh,''))>0");
                    var lstWords = data.MapperList<string>("zh");
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }
                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_word_type Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
                else
                {
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }

                        var data = DbTop.GetDataTable("Select zh From sys_word_type Where length(ifnull(zh,''))>0 and length(ifnull(" + lg + ",''))<=0 ");
                        var lstWords = data.MapperList<string>("zh");

                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_word_type Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
            }
            else if (type.Equals("word", StringComparison.OrdinalIgnoreCase))
            {
                if (isReplace)
                {
                    var data = DbTop.GetDataTable("Select zh From sys_word Where length(ifnull(zh,''))>0");
                    var lstWords = data.MapperList<string>("zh");
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }
                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_word Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
                else
                {
                    StringBuilder sbSql = new StringBuilder();
                    foreach (var strEnum in lstEnum)
                    {
                        Lang lg = (Lang)Enum.Parse(typeof(Lang), strEnum);
                        if (lg == Lang.zh)
                        {
                            continue;
                        }

                        var data = DbTop.GetDataTable("Select zh From sys_word Where length(ifnull(zh,''))>0 and length(ifnull(" + lg + ",''))<=0 ");
                        var lstWords = data.MapperList<string>("zh");

                        var lstNewWord = FanYinUtil.Trans(lstWords, lg);
                        for (int j = 0; j < lstWords.Count; j++)
                        {
                            sbSql.AppendLine("Update sys_word Set " + lg + "=N'" + lstNewWord[j] + "' Where zh=N'" + lstWords[j] + "';");
                        }
                    }
                    string upSql = sbSql.ToString();
                    if (string.IsNullOrWhiteSpace(upSql))
                    {
                        return true;
                    }
                    int res = DbTop.ExecSql(upSql);
                }
            }
            return true;
        }
    }
}
